/**
 * 问题详情
 */
var localStorageManagers = require('script/localStorageManagers.js');
//初始化mui
mui.init({
	swipeBack: true
});
FastClick.attach(document.body);
//初始化plus
mui.plusReady(function() {
	plusReady();
});
//plusReady
function plusReady() {
	//滚动条
	mui('.mui-scroll-wrapper').scroll({
					indicators: false //是否显示滚动条
				});
	//加载问题
	loadQuestionDetail();
	//新增问题
	mui('.footbtndiv').on('tap', '.footbtnspan', openPutques);
	mui('.mui-content').on('tap', '.childanswerbtn', openPutques1);
	//保存提问
	mui('.putques-div').on('tap', '.submitques', savePutques);
	//取消提问
	mui('.putques-div').on('tap', '.cancelques', cancelQues);
}
//加载问题详情
function loadQuestionDetail() {
	document.getElementById('questions').innerHTML=' ';
	ajaxLoadQuestionDetail();
};
//ajax加载问题详情
function ajaxLoadQuestionDetail() {
	var self = plus.webview.currentWebview();
	var quesid = self.quesid;
	var url = serverAjax + "/mobile/studycenter/getQuestionDetailByQuesid.do";
	mui.ajax(url, {
		data: {
			quesid: quesid
		},
		type: 'post',
		dataType: 'json',
		timeout: 10000,
		success: function(data) {
			if (data != null && data.length > 0){ 
				buildQuestionDiv(data);
			    //mui('.elseinfo').imageLazyload({
				//   placeholder: '../../images/user_guest.png'
		    	//});
			}
		},
		error: function(e1, e2, e3) {
			ajaxErrorTip();
			console.log(e1);
		}
	});
};
//构建问题信息div
function buildQuestionDiv(quesList) {
	var questionsEle = document.getElementById('questions');
	for (var i = 0; i < quesList.length; i++) {
		var question = quesList[i];
		var parentid = question.parentId;
		var content=question.content;
		if(content.indexOf('~')!=-1){
			content='<span style="color:#7b7b7b;margin-right:10px;">'+content.substring(0,content.indexOf('~'))+":</span>"+content.substring(content.indexOf('~')+2,content.length);
		}
		if (parentid == 0) {
			var replynum = quesList.length - 1;
			var questionsdiv = document.getElementById('questions');
			var imgsrc="../../images/user_guest.png";
		    if(question.userpath!=null&&question.userpath.length>0)imgsrc=question.userpath;
	
			var questionstr = '<div class="question">'
			questionstr += '<div class="ques-content">'
			questionstr += '<div class="elseinfo"><span class="userimg"><img src="'+imgsrc+'"></span>'
			questionstr += '<p class="p1">' + question.creator + '<span class="c3">回复</span><span class="c2">' + replynum + '</span></p>' + '<p class="p2"><span class="c1">' + question.createTime + '</span>'
			questionstr += '</p>' + '</div>' + '<div class="content-value">' + content + '</div>'
			if (question.coursename != null && question.coursename.length > 0) questionstr += '<div class="courseinfo"><span>课程:</span><span class="t2">' + question.coursename + '</span><span>课题:</span><span class="t2">' + question.catalogname + '</span><span>资源:</span><span class="t2">' + question.resname + '</span></div>'
			questionstr += '<div class="reply-div" questionid="' + question.qstId + '"><img src="../../images/reply.png"></div>' + '</div>'
			questionstr += '<div class="ques-answers" id="parentquestionid_' + question.qstId + '">' + '</div>' + '</div>';
			questionsdiv.innerHTML = questionsdiv.innerHTML + questionstr;
			document.getElementById('fParentid').value=question.qstId;
			document.getElementById('fCourseid').value=question.courseId;
	        document.getElementById('fCatalogid').value=question.catalogId;
	        document.getElementById('fResid').value=question.resId;
		} else {
			var parentquestioniddiv = document.getElementById('parentquestionid_' + question.parentId);
			//第一层提问
			if (parentquestioniddiv != null) {
				var teachertag = '';
			    if (question.answerType == 0) teachertag = '(老师)';
				var questionstr = '<div class="answer" id="erjiquestionid_' + question.qstId + '">' + '<div class="answeruser"><span>' + question.creator +teachertag+ '</span><span class="date">' + question.createTime + '</span><span class="childanswerbtn" questionid="'+question.qstId+'" reply="'+question.creator+teachertag+'">回复</span></div>' + '<div class="answer-content"><span class="hfc">' + content + '</span><div class="reply-div"><img src="../../images/reply.png"></div></div>' + '</div>';
				parentquestioniddiv.innerHTML = parentquestioniddiv.innerHTML + questionstr;
			} else {
				//第二层
				var parentquestioniddiv2 = document.getElementById('erjiquestionid_' + question.parentId);
				if (parentquestioniddiv2 != null) {
					//var questionstr = '<div class="answer" id="erjiquestionid_' + question.id + '">' + '<div class="answeruser"><span>' + question.creator + '</span><span class="date">' + question.createTime + '</span></div>' + '<div class="answer-content"><span class="hfc">' + question.content + '</span></div>' + '</div>';
					var questionstr = '<div class="answeruser"><span>' + question.creator + '</span><span class="date">' + question.createTime + '</span></div>' + '<div class="answer-content"><span class="hfc">' + content + '</span></div>';
					//parentquestioniddiv2.parent.innerHTML = parentquestioniddiv2.parent.innerHTML + questionstr;
					var newdiv = document.createElement("div");
					newdiv.className = 'answer';
					newdiv.id = 'erjiquestionid_' + question.qstId + '';
					newdiv.innerHTML = questionstr;
					//insertAfter(newdiv, parentquestioniddiv2);
					//parentquestioniddiv2.parentNode.innerHTML=parentquestioniddiv2.parentNode.innerHTML+questionstr;
				}
			}
		}
	}
}
//后面插入
function insertAfter(newEl, targetEl) {
	var parentEl = targetEl.parentNode;

	if (parentEl.lastChild == targetEl) {
		parentEl.appendChild(newEl);
	} else {
		parentEl.insertBefore(newEl, targetEl.nextSibling);
	}
}
//打开新增问题框
function openPutques() {
	replyto='';
	document.getElementById('questextarea').value = '';
	document.getElementById('putques-div').style.display = 'block';
};
//打开新增问题框
var replyto='';
function openPutques1() {
	
	var reply=this.getAttribute('reply');
	replyto="回复 "+reply+"~     ";
	//document.getElementById('questextarea').value = "回复 "+reply+"~     ";
	document.getElementById('questextarea').value = '';
	document.getElementById('questextarea').placeholder="回复 "+reply+":";
	document.getElementById('putques-div').style.display = 'block';
};
//取消问题回复框
function cancelQues(){
	document.getElementById('questextarea').placeholder="请输入您的回复...";
	document.getElementById('putques-div').style.display = 'none';
};
//保存问题
function savePutques() {
	
	var userid = localStorageManagers.getUseridSession();
	var courseid = document.getElementById('fCourseid').value;
	var catalogid = document.getElementById('fCatalogid').value;
	var resid = document.getElementById('fResid').value;
	var quesContent = replyto+document.getElementById('questextarea').value;
	var parentId=document.getElementById('fParentid').value;
	if (quesContent.length == 0) {
		mui.toast("请填写问题内容");
		return;
	}
	if (quesContent.length > 200) {
		mui.toast("最多不超过200字！");
		return;
	}
	var data={
			userId: userid,
			resId: resid,
			catalogId: catalogid,
			courseId: courseid,
			questionContent: quesContent,
			parentId:parentId
	};
	ajaxSubmitQuestion(data);
	document.getElementById('questextarea').placeholder="请输入您的回复...";
	document.getElementById('putques-div').style.display = 'none';
}
//ajax保存问题
function ajaxSubmitQuestion(data){
	mui.ajax(serverAjax + '/mobile/question/SaveQuestion.do', {
		type: 'post',
		dataType: 'json',
		data: data,
		success: function(result) {
             if(result!=0){
             	mui.toast('回复成功');
             	buildQuestionDiv(result);
             	//loadQuestionDetailresult);selfcenter/question/questionindex
             	var parentWebView = plus.webview.getWebviewById("selfcenter/question/questionindex");
	            parentWebView.evalJS("addAnswerNum('" + result[0].parentId + "')");
             }else{
             	mui.toast('回复失败');
             }
		},
		error: function(xhr, type, errorThrown) {
			ajaxErrorTip();
			console.log(xhr);
		}
	});
};